﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;

namespace PrinterBudi.Persistence
{
    public class PBTransactionModel
    {
        public static void Delete(PBTransaction existingTransaction)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(existingTransaction);
                    transaction.Commit();
                }
            }
        }

        public static void Update(PBTransaction newTransactionName)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(newTransactionName);
                    transaction.Commit();
                    Console.WriteLine("Updated Transaction: " + newTransactionName.Id);
                }
            }
        }

        public static PBTransaction Read(int transactionId)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.Id == transactionId
                                        select transaction).Single();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> ReadAllQueued()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.Status == "queued"
                                        select transaction). ToList();
                
                return transactionQuery;
            }
        }

        public static List<PBTransaction> ReadAllPrinted()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.Status == "printed"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> ReadAllDelivered()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.Status == "delivered"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static void Create(DateTime uploadDate, string fileName, string status, string description, string destination, int totalPrice, string paperType, string paperSize, int numCopy, DateTime deliveredDate, PBUser user)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var transTemp = new PBTransaction
                    {
                        UploadDate = uploadDate,
                        FileName = fileName,
                        Status = status,
                        Destination = destination,
                        Description = description,
                        TotalPrice = totalPrice,
                        PaperType = paperType,
                        PaperSize = paperSize,
                        NumCopy = numCopy,
                        DeliveredDate = deliveredDate,
                        PBUser = user
                    };
                    session.Save(transTemp);

                    transaction.Commit();
                    Console.WriteLine("Created Transaction: " + transTemp.Id);

                }
            }
        }

        public static void SetPrinted(PBTransaction existingTransaction, PBEmployee employeeNow)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    existingTransaction.Status = "printed";
                    existingTransaction.PBEmployee = employeeNow;
                    existingTransaction.DeliveredDate = DateTime.Parse("9999-12-31 0:0:0");
                    session.SaveOrUpdate(existingTransaction);
                    transaction.Commit();
                    Console.WriteLine("Updated Transaction: " + existingTransaction.Id);
                }
            }
        }

        public static void SetDelivered(PBTransaction existingTransaction, PBEmployee employeeNow)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    existingTransaction.Status = "delivered";
                    existingTransaction.PBEmployee = employeeNow;
                    existingTransaction.DeliveredDate = DateTime.Now;
                    session.SaveOrUpdate(existingTransaction);
                    transaction.Commit();
                    Console.WriteLine("Updated Transaction: " + existingTransaction.Id);
                }
            }
        }

        public static List<PBTransaction> SearchQByPhoneNumber(string phoneNumber)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.PBUser.PhoneNumber == phoneNumber && transaction.Status == "queued"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> SearchQByDate(DateTime dateFrom, DateTime dateTo)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.UploadDate >= dateFrom && transaction.UploadDate <= dateTo && transaction.Status == "queued"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> SearchPByDate(DateTime dateFrom, DateTime dateTo)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.UploadDate >= dateFrom && transaction.UploadDate <= dateTo && transaction.Status == "printed"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> SearchDByDate(DateTime dateFrom, DateTime dateTo)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.UploadDate >= dateFrom && transaction.UploadDate <= dateTo && transaction.Status == "delivered"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> SearchPByPhoneNumber(string phoneNumber)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                       where transaction.PBUser.PhoneNumber == phoneNumber && transaction.Status == "printed"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }

        public static List<PBTransaction> SearchDByPhoneNumber(string phoneNumber)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transactionQuery = (from transaction in session.Query<PBTransaction>()
                                        where transaction.PBUser.PhoneNumber == phoneNumber && transaction.Status == "delivered"
                                        select transaction).ToList();

                return transactionQuery;
            }
        }
    }
}
